//
//  TQPickerView.m
//  CRM
//
//  Created by teeking_scx on 2018/1/3.
//  Copyright © 2018年 teeking_scx. All rights reserved.
//

#import "TQPickerView.h"
@interface TQPickerView()<UIPickerViewDelegate,UIPickerViewDataSource>
@property (nonatomic,strong) UIView *toolView;
@property (nonatomic,strong) UIPickerView *pickerView;
@property (nonatomic,strong) UIButton *cancelBtn;
@property (nonatomic,strong) UILabel *titleLabel;
@property (nonatomic,strong) UIButton *enterBtn;
@property (nonatomic,strong) NSMutableArray *dataSource;
@property (nonatomic,copy) NSString *selectedStr;
@property (nonatomic,assign) NSInteger index;
@end

@implementation TQPickerView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initSubView:frame];
    }
    return self;
}

-(void)initSubView:(CGRect)frame{
    self.backgroundColor = [UIColor colorWithWhite:0.3 alpha:0.5];
    
    self.toolView = [[UIView alloc]initWithFrame:CGRectMake(0,frame.size.height, frame.size.width, 250)];
    self.toolView.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.toolView];
    
    self.cancelBtn = [[UIButton alloc]initWithFrame:CGRectMake(10, 5, 40, 30)];
    [self.cancelBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [self.cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [self.cancelBtn addTarget:self action:@selector(cancel) forControlEvents:UIControlEventTouchUpInside];
    [self.toolView addSubview:self.cancelBtn];
    
    self.titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(60, 5, ScreenWidth - 120, 30)];
    self.titleLabel.textColor = HexRGB(0x666666);
    self.titleLabel.font = Font14;
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    [self.toolView addSubview:self.titleLabel];
    
    self.enterBtn = [[UIButton alloc]initWithFrame:CGRectMake(self.width - 50, 5, 40, 30)];
    [self.enterBtn setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
    [self.enterBtn setTitle:@"确定" forState:UIControlStateNormal];
    [self.enterBtn addTarget:self action:@selector(enter) forControlEvents:UIControlEventTouchUpInside];
    [self.toolView addSubview:self.enterBtn];
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.cancelBtn.frame)+ 5, ScreenWidth, 1)];
    view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.toolView addSubview:view];
    
    self.pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0,CGRectGetMaxY(view.frame), ScreenWidth, self.toolView.height - CGRectGetMaxY(view.frame))];
    self.pickerView.delegate = self;
    self.pickerView.dataSource = self;
//    self.pickerView.showsSelectionIndicator = YES;
    [self.toolView addSubview:self.pickerView];
    
    [UIView animateWithDuration:0.2 animations:^{
        self.toolView.y = ScreenHeight - 250;
    }];
    if (self.dataSource.count > 0) {
        self.selectedStr = self.dataSource[0];
    }
    
}

+(instancetype)showDataSource:(NSArray *)dataSource{
    
    TQPickerView *pickerView = [[TQPickerView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
    pickerView.dataSource = [dataSource copy];
    if (dataSource.count > 0) {
        pickerView.index = 0;
        pickerView.selectedStr = dataSource[0];
    }
    UIWindow *window = KeyWindow;
    [window addSubview:pickerView];
    return pickerView;
}
-(void)setTitle:(NSString *)title{
    _title = title;
    self.titleLabel.text = title;
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return self.dataSource.count;
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    return self.dataSource[row];
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    self.index = row;
    self.selectedStr = self.dataSource[row];
}
-(void)cancel{
    [self removeView];
}
-(void)hide{
    [self removeView];
}

-(void)enter{
    if (self.selectedBlock) {
        self.selectedBlock(self.selectedStr,self.index);
    }
    [self removeView];
}
-(void)removeView{
    [UIView animateWithDuration:0.2 animations:^{
        self.toolView.y = ScreenHeight;
    } completion:^(BOOL finished) {
        if (finished) {
            [self removeFromSuperview];
        }
    }];
}

-(void)dealloc{
    NSLog(@"%s",__func__);
}

@end
